package po

import (
	"GameGo/config"
	"golang.org/x/image/math/f64"
	"math"
)

type Boss struct {
	OrdinaryEnemy
	TargetPosition  f64.Vec2
	EnemySkill1     int
	EnemySkill2     int
	Fire2CD         int
	EnemySkill3     int
	Fire3CD         int
	LaserChargeTime int      // 蓄力倒计时
	LaserFired      bool     // 是否已经发射
	LaserDir        f64.Vec2 // 预先计算的发射方向
}

func NewBoss(level int) *Boss {
	enemy := &Boss{
		OrdinaryEnemy: OrdinaryEnemy{
			EnemyLevel: level,
			X:          config.ScreenXSize / 2,
			Y:          -60,
			FireCD:     10,
			MoveType:   BossT,
			EnemyDir:   DirFromAngle(270),
			IsShotgun:  false,
		},
		TargetPosition: f64.Vec2{config.ScreenXSize/2 - 60, config.ScreenYSize / 4},
		EnemySkill1:    120,
		EnemySkill2:    120,
		Fire2CD:        10,
		EnemySkill3:    60,
		Fire3CD:        10,
		LaserFired:     false,
	}
	enemy.EnemyMaxHP = enemy.EnemyLevel * config.EnemyHP * 10
	enemy.EnemyHP = enemy.EnemyMaxHP
	return enemy
}
func (e *Boss) EnemyMove() {
	if IsColliding(e.X, e.Y, 65, 65, e.TargetPosition[0]+60, e.TargetPosition[1], 30, 30) {
		e.IsShotgun = true
		return
	}
	target := e.TargetPosition
	current := f64.Vec2{e.EnemyDir[0], e.EnemyDir[1]}
	toTarget := f64.Vec2{
		target[0] - e.X,
		-(target[1] - e.Y),
	}
	maxAnglePerFrame := 0.05
	e.EnemyDir = TurnToward(current, toTarget, maxAnglePerFrame)
	e.Y -= 2 * e.EnemyDir[1]
	e.X += 2 * e.EnemyDir[0]

}
func (e *Boss) EnemyShot(Bullets *[]Bullet, gx, gy float64) {
	if e.EnemySkill1 > 0 {
		// skill1 执行
		e.Skill1(Bullets, gx, gy)
	} else if e.EnemySkill2 > 0 {
		// skill2 执行
		e.Skill2(Bullets)
		// skill3 执行
	} else if e.EnemySkill3 > 0 {
		// skill2 执行
		e.Skill3(Bullets, gx, gy)
		// skill3 执行
	} else {
		// 重置技能或等待下一轮
		e.EnemySkill1 = 60
		e.EnemySkill2 = 60
		e.EnemySkill3 = 60
	}

}
func (e *Boss) Skill1(Bullets *[]Bullet, gx, gy float64) {
	if !e.IsShotgun {
		return
	}
	e.FireCD--
	if e.EnemySkill1 == 0 {
		return
	} else {
		e.EnemySkill1--
	}
	if e.FireCD <= 0 {

		bullet := Bullet{
			BulletType:     EnemyBullet,
			BulletMoveType: Linear,
			BulletSpeed:    6.0,
			Damage:         e.EnemyLevel * 2,
			Life:           180,
			Scale:          1,
		}
		bullet.Position = f64.Vec2{e.X, e.Y + 30}
		bullet.Dir = DirFromAngle(270)
		var bullets [7]Bullet
		for i := range bullets {
			bullets[i] = bullet
			bullets[i].Dir = DirFromAngle(float64(180 + i*30))
		}
		*Bullets = append(*Bullets, bullets[:]...)
		e.FireCD = 5
	}
}
func (e *Boss) Skill2(Bullets *[]Bullet) {
	if !e.IsShotgun {
		return
	}
	e.Fire2CD--
	if e.EnemySkill2 == 0 {
		return
	} else {
		e.EnemySkill2--
	}
	if e.Fire2CD <= 0 {

		bullet := Bullet{
			BulletType:     EnemyBullet,
			BulletMoveType: Linear,
			BulletSpeed:    5.0,
			Damage:         e.EnemyLevel * 2,
			Life:           120,
			Scale:          1,
		}
		bullet.Position = f64.Vec2{e.X, e.Y + 30}
		bullet.Dir = DirFromAngle(270)
		const numBullets = 36
		for i := 0; i < numBullets; i++ {
			b := bullet
			angle := float64(i) * (360.0 / numBullets)
			b.Dir = DirFromAngle(angle)
			*Bullets = append(*Bullets, b)
		}
		e.Fire2CD = 30
	}
}
func (e *Boss) Skill3(Bullets *[]Bullet, gx, gy float64) {
	if !e.IsShotgun {
		return
	}
	// Skill3: 蓄力激光
	if e.EnemySkill3 > 0 {
		if !e.LaserFired {
			if e.LaserChargeTime == 0 {
				// 进入蓄力阶段，计算激光方向
				dx := gx - e.X
				dy := gy - e.Y
				l := math.Hypot(dx, dy)
				e.LaserDir = f64.Vec2{dx / l, -dy / l}
				e.LaserChargeTime = 60 // 1秒蓄力（约60帧）
				e.FireCD = 0
			} else {
				e.LaserChargeTime--
				if e.LaserChargeTime == 0 {
					// 发射激光
					laser := Bullet{
						BulletType:     EnemyBullet,
						BulletMoveType: Linear,
						BulletSpeed:    20,
						Damage:         e.EnemyLevel * 6, // 高伤
						Life:           60,
						Dir:            e.LaserDir,
						Position:       f64.Vec2{e.X, e.Y},
						Scale:          1.5, // 激光更粗
					}
					*Bullets = append(*Bullets, laser)
					e.LaserFired = true
					e.FireCD = 30
				}
			}
		} else {
			e.EnemySkill3--
			if e.EnemySkill3 == 0 {
				e.LaserFired = false
			}
		}
		return
	}

}
